package com.example.a80205050.myapplication.MyActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.a80205050.myapplication.Dao.FlagDao;
import com.example.a80205050.myapplication.Dao.InaccountDao;
import com.example.a80205050.myapplication.Dao.OutaccountDao;
import com.example.a80205050.myapplication.Modle.Tb_flag;
import com.example.a80205050.myapplication.Modle.Tb_inaccount;
import com.example.a80205050.myapplication.Modle.Tb_outaccount;
import java.util.List;

/**
 * Created by 80205050 on 2017/11/1.
 */

public class Showinfo extends Activity {
    public static final String FLAG="id";
    Button btnoutinfo,btnininfo,btnflaginfo;
    ListView lvinfo;
    String strType="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showinfo);
        lvinfo=(ListView)findViewById(R.id.lvinfo);
        btnoutinfo=(Button)findViewById(R.id.btnoutinfo);
        btnininfo=(Button)findViewById(R.id.btnininfo);
        btnflaginfo=(Button)findViewById(R.id.btnflaginfo);
        showInfo(R.id.btnoutinfo);//默认显示的是支出的信息
        btnoutinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showInfo(R.id.btnoutinfo);
            }
        });
        btnininfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showInfo(R.id.btnininfo);
            }
        });
        btnflaginfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showInfo(R.id.btnflaginfo);
            }
        });
        lvinfo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String strInfo=String.valueOf(((TextView)view).getText());
                String strId=strInfo.substring(0,strInfo.indexOf("|"));
                Intent intent=null;
                if(strType.equals("btnininfo")||strType.equals("btnoutinfo")){
                    intent=new Intent(Showinfo.this,InfoManage.class);
                    intent.putExtra(FLAG,new String[]{strId,strType});
                }else if(strType.equals("btnflaginfo")){
                    intent=new Intent(Showinfo.this,FlagManage.class);
                    intent.putExtra(FLAG, strId);// 设置要传递的数据
                }
                startActivity(intent);

            }
        });

    }

    private void showInfo(int type){
        String[] strInfos=null;
        ArrayAdapter<String> arrayAdapter=null;
        switch (type){
            case  R.id.btnoutinfo:
                strType="btnoutinfo";
                OutaccountDao outaccountDao=new OutaccountDao(Showinfo.this);
                List<Tb_outaccount> lisoutinfos=outaccountDao.getScrollData(0,(int)outaccountDao.getCount());
                strInfos=new String[lisoutinfos.size()];
                int i=0;
                for(Tb_outaccount tb_outaccount:lisoutinfos){
                    strInfos[i]=tb_outaccount.get_id()+"|"+tb_outaccount.getType()+
                            " "+String.valueOf(tb_outaccount.getMoney())+"元  "+tb_outaccount.getTime();
                    i++;
                }
                break;
            case  R.id.btnininfo:
                strType="btnininfo";
                InaccountDao inaccountDao=new InaccountDao(Showinfo.this);
                List<Tb_inaccount> lisininfos=inaccountDao.getScrollData(0,(int)inaccountDao.getCount());
                strInfos=new String[lisininfos.size()];
                int j=0;
                for(Tb_inaccount tb_inaccount:lisininfos){
                    strInfos[j]=tb_inaccount.get_id()+"|"+tb_inaccount.getType()+
                            " "+String.valueOf(tb_inaccount.getMoney())+"元  "+tb_inaccount.getTime();
                    j++;
                }
                break;
            case  R.id.btnflaginfo:
                strType="btnoutinfo";
                FlagDao flagDao=new FlagDao(Showinfo.this);
                List<Tb_flag> lisflaginfos=flagDao.getScrollData(0,(int)flagDao.getCount());
                strInfos=new String[lisflaginfos.size()];
                int k=0;
                for(Tb_flag tb_flag:lisflaginfos){
                    strInfos[k]=tb_flag.get_id()+"|"+tb_flag.getFlag();
                    if(strInfos[k].length()>15){
                        strInfos[k]=strInfos[k].substring(0,15)+".....";
                    }
                    k++;
                }
                break;
            default:
        }
        arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,strInfos);
        lvinfo.setAdapter(arrayAdapter);
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        showInfo(R.id.btnoutinfo);//显示支出的信息
    }
}
